package com.me.Inheritor;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;

public class TextureManager 
{
	class UniqueTexture
	{
		public Texture texture;
		public String name;
	}
	
	ArrayList< UniqueTexture > textures;
	
	TextureManager()
	{
		textures = new ArrayList< UniqueTexture >();
	}
	
	public Texture getTexture( int id )
	{
		if( id != -1 )
			return textures.get(id).texture;
		return null;
	}
	
	// Loads a texture and returns an id : id is -1 if it failed to load.
	public int LoadTexture( String FileName )
	{
		int id = CheckTexture( FileName );
		
		if( id != -1 )
			return id;
		
		id = textures.size();
		
		Texture tex = null;
		try
		{
			tex = new Texture( Gdx.files.internal(FileName) );
		}
		finally
		{
			if( tex == null )
			{
				System.out.println("Error loading texture '" + FileName + "'." );
				return -1;
			}
		}
		
		// The texture is valid let's add it to the list
		UniqueTexture ut = new UniqueTexture();
		ut.texture = tex;
		ut.name = FileName;
		
		textures.add( ut );
		
		return id;
	}
	
	// Checks to see if the texture already exists, returns -1 if it doesn't.
	public int CheckTexture( String FileName )
	{
		for( int i = 0;i < textures.size();++i )
		{
			UniqueTexture ut = textures.get(i);
			
			if( ut.name.equals(FileName) )
				return i;
		}
		
		return -1;
	}
	
	public void Dispose()
	{
		for( int i = 0;i < textures.size();++i )
			textures.get(i).texture.dispose();
	}
}
